package com.mytinyRPC.register.zookeeperRegister;


import com.mytinyRPC.framework.URL;
import com.mytinyRPC.register.ServiceDiscovery;

import java.net.InetSocketAddress;
import java.util.LinkedList;
import java.util.List;


public class ZkServiceDiscovery implements ServiceDiscovery {
    public ZkServiceDiscovery() {
    }

    /***
     * @Author HP
     * @Description //TODO 获取URL列表
     * @Name getURL
     * @Param [interfaceName]
     * @return java.util.List<framework.URL>
     **/
    @Override
    public List<URL> getURL(String interfaceName){
        List<String> serviceAddresses = CuratorUtils.getChildrenNodes(interfaceName);
        LinkedList<URL> urls=new LinkedList<>();
        for (String str:serviceAddresses){
            urls.add(change(str));
        }
        return urls;
    }
    /***
     * @Author HP
     * @Description //String封装为URL对象
     * @Name change
     * @Param [serviceAddress]
     * @return framework.URL
     **/
    public static URL change(String serviceAddress){
        String[] socketAddressArray = serviceAddress.split(":");
        String host = socketAddressArray[0];
        int port = Integer.parseInt(socketAddressArray[1]);
        int weight = Integer.parseInt(socketAddressArray[2]);
        URL url=new URL(host,port,weight);
        return url;
    }
}
